package com.huawei.hms.framework.network.grs.b;

import android.os.SystemClock;
import com.huawei.hms.framework.common.IoUtils;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.grs.c.a.a;
import com.huawei.hms.framework.network.grs.c.b;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.concurrent.Callable;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;

public class d implements Callable<c> {
    private static final String a = d.class.getSimpleName();
    private String b;
    private a c;
    private c d;
    private int e;

    public d(String str, int i, a aVar) {
        this.b = str;
        this.c = aVar;
        this.e = i;
    }

    private static SSLSocketFactory b() {
        try {
            return a.a(b.a(), "grs_sp.bks");
        } catch (IOException e) {
            throw new AssertionError(e);
        } catch (CertificateException e2) {
            throw new AssertionError(e2);
        } catch (NoSuchAlgorithmException e3) {
            throw new AssertionError(e3);
        } catch (IllegalAccessException e4) {
            throw new AssertionError(e4);
        } catch (KeyStoreException e5) {
            throw new AssertionError(e5);
        } catch (KeyManagementException e6) {
            throw new AssertionError(e6);
        }
    }

    public c a() {
        Throwable e;
        long j;
        long elapsedRealtime;
        InputStream inputStream = null;
        Logger.i(a, "call execute");
        try {
            long elapsedRealtime2 = SystemClock.elapsedRealtime();
            try {
                URLConnection openConnection = new URL(this.b).openConnection();
                if (openConnection instanceof HttpsURLConnection) {
                    byte[] bArr;
                    HttpsURLConnection httpsURLConnection = (HttpsURLConnection) openConnection;
                    try {
                        httpsURLConnection.setSSLSocketFactory(b());
                        httpsURLConnection.setHostnameVerifier(a.b);
                    } catch (IllegalArgumentException e2) {
                        Logger.w(a, "init https ssl socket failed.");
                    }
                    httpsURLConnection.setRequestMethod(HttpContants.HTTP_METHOD_GET);
                    httpsURLConnection.setConnectTimeout(10000);
                    httpsURLConnection.setReadTimeout(10000);
                    httpsURLConnection.setRequestProperty("User-Agent", com.huawei.hms.framework.network.grs.c.a.b(b.a(), "NetworkKit-grs"));
                    httpsURLConnection.connect();
                    int responseCode = httpsURLConnection.getResponseCode();
                    if (responseCode == 200) {
                        inputStream = openConnection.getInputStream();
                        byte[] toByteArray = IoUtils.toByteArray(inputStream);
                        IoUtils.closeSecure(inputStream);
                        bArr = toByteArray;
                    }
                    this.d = new c(responseCode, openConnection.getHeaderFields(), bArr, SystemClock.elapsedRealtime() - elapsedRealtime2);
                    this.d.a(this.b);
                    this.d.a(this.e);
                    if (this.c != null) {
                        this.c.a(this.d);
                    }
                    return this.d;
                }
                Logger.w(a, "urlConnection is not an instance of HttpsURLConnection");
                return null;
            } catch (IOException e3) {
                e = e3;
                j = elapsedRealtime2;
                elapsedRealtime = SystemClock.elapsedRealtime();
                Logger.w(a, "RequestCallable run task catch IOException", e);
                this.d = new c(e, elapsedRealtime - j);
                this.d.a(this.b);
                this.d.a(this.e);
                if (this.c != null) {
                    this.c.a(this.d);
                }
                return this.d;
            } catch (Throwable th) {
                IoUtils.closeSecure(inputStream);
            }
        } catch (IOException e4) {
            e = e4;
            j = 0;
            elapsedRealtime = SystemClock.elapsedRealtime();
            Logger.w(a, "RequestCallable run task catch IOException", e);
            this.d = new c(e, elapsedRealtime - j);
            this.d.a(this.b);
            this.d.a(this.e);
            if (this.c != null) {
                this.c.a(this.d);
            }
            return this.d;
        }
    }

    public /* synthetic */ Object call() {
        return a();
    }
}
